package com.yuer.zk.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static org.apache.zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL;
import static org.apache.zookeeper.ZooDefs.Ids.ANYONE_ID_UNSAFE;

public class ZkCreateTest implements Watcher {

    static CountDownLatch countDownLatch = new CountDownLatch(1);
    private  static ZooKeeper zooKeeper;
    public static void main(String[] args) {
        try {
              zooKeeper =  new ZooKeeper("192.168.56.103:2182",5000,new ZkCreateTest());
            System.out.println("此时的链接状态：  "+zooKeeper.getState());
            Thread.sleep(Integer.MAX_VALUE);
//            countDownLatch.await();
            System.out.println("此时的链接状态---->：  "+zooKeeper.getState());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent event) {

        if(event.getState() == Event.KeeperState.SyncConnected){
            countDownLatch.countDown();
            System.out.println("方法执行中....."+event.getState());
            createNode();
        }
//        createNode();
    }
    private void createNode(){
        /**
         * path ：节点创建的路径
         * data[] ：节点创建要保存的数据，是个byte类型的
         * acl ：节点创建的权限信息(4种类型)
         * ANYONE_ID_UNSAFE : 表示任何⼈
         * AUTH_IDS ：此ID仅可⽤于设置ACL。它将被客户机验证的ID替
         换。
         * OPEN_ACL_UNSAFE ：这是⼀个完全开放的ACL(常⽤)-->
         world:anyone
         * CREATOR_ALL_ACL ：此ACL授予创建者身份验证ID的所有权限
         * createMode ：创建节点的类型(4种类型)
         * PERSISTENT：持久节点
         * PERSISTENT_SEQUENTIAL：持久顺序节点
         * EPHEMERAL：临时节点
         * EPHEMERAL_SEQUENTIAL：临时顺序节点
         String node = zookeeper.create(path,data,acl,createMode)*/

//        zooKeeper.create(
//        final String path,
//        byte[] data,
//        List<ACL> acl,
//        CreateMode createMode);
        try {
           String  result_persistent =  zooKeeper.create("/zok-test—persistent","持久节点的内容".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
            System.out.println("结果： " +result_persistent);
            String  result_persistent_sequential =  zooKeeper.create("/zok-test-persistent_sequential","持久节点--有序-内容".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT_SEQUENTIAL);
            String  result_ephemeral_sequential =    zooKeeper.create("/zok-test-ephemeral_sequential","临时节点的内容-有序".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, EPHEMERAL_SEQUENTIAL);
            String  result_ephemeral =   zooKeeper.create("/zok-test-result_ephemeral","临时节点的内容".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
            System.out.println(result_persistent);
            System.out.println(result_persistent_sequential);
            System.out.println(result_ephemeral_sequential);
            System.out.println(result_ephemeral);

        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
